12. Performance Considerations

Performance Considerations

Until now, our reasons for using one data structure over another have mostly had to do with convenience and readability. But there's another reason to use dictionaries and sets over lists. Speed!

In the rest of this lesson you'll see how fast dictionaries and sets can be.

Note that when we talk about a data structure being "fast", we'll be talking about how long it takes to perform a membership test with Python's in keyword.

Here's a brief reminder of how membership testing works…

…on lists

> my_list = [1,2,3]
> 1 in my_list
True
> 4 in my_list
False

…on dictionaries

> my_dictionary = {1: 'one', 2: 'two', 3: 'three'}
> 1 in my_dictionary
True
> 'one' in my_dictionary
False

…on sets

> my_set = set([1, 2, 3])
> 1 in my_set
True
> 'one' in my_set
False

In the next section you'll explore how the size of a list impacts its speed.